package com.cjq.allservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjq.allservice.entity.Activity;
import com.cjq.allservice.entity.Member;
import com.cjq.allservice.service.ActivityService;
import com.cjq.allservice.service.MemberService;
import com.cjq.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjq
 * @since 2022-01-11
 */
@RestController
@RequestMapping("/allservice/member")
@CrossOrigin
public class MemberController {

    @Autowired
    private MemberService memberService;

    private final ActivityService activityService;
    public MemberController(ActivityService activityService){
        this.activityService=activityService;
    }

    @ApiOperation("用户进行组织活动报名")
    @PostMapping("/addMember")
    public R addMember(@RequestBody Member member){

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("member_activity_id",member.getMemberActivityId());
        List list = memberService.list(wrapper);
        int i = list.size();
        Activity activity = activityService.getById(member.getMemberActivityId());
        if (activity.getActivityNumber()>i){//进行判断组织活动限制人数是否超过
            boolean save = memberService.save(member);
            activity.setMemberNumber(activity.getMemberNumber()+1);//进行已报名人数统计
            activityService.updateById(activity);
            if (save==true){
                return R.succ().message("添加成功！");
            }else {
                return R.fail().message("添加失败！");
            }
        }else {
            return R.fail().message("该组织活动人数已满！");
        }
    }

    @ApiOperation("进行组织活动的取消报名")
    @PostMapping("deleteMemberById")
    public R deleteMemberById(@RequestParam String activityId,@RequestParam String userId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("member_activity_id",activityId);
        //wrapper.eq("member_user_id",userId);
        Activity activity = activityService.getById(activityId);
        activity.setMemberNumber(activity.getMemberNumber()-1);
        boolean b = activityService.updateById(activity);
        QueryWrapper wrapper1 = new QueryWrapper();
        wrapper1.eq("member_activity_id",activityId);
        wrapper1.eq("member_user_id",userId);
        boolean remove = memberService.remove(wrapper1);
        if (b&&remove){
            return R.succ().message("取消报名成功！");
        }else{
            return R.fail().message("取消报名失败！");
        }
    }



    @ApiOperation("判断是否已经报名并返回标志位")
    @GetMapping("/getFlagMember")
    public R getFlagMember(@RequestParam String userId,@RequestParam String activityId){
        QueryWrapper<Member> wrapper = new QueryWrapper();
        wrapper.eq("member_activity_id",activityId);
        wrapper.eq("member_user_id",userId);
        Member member = memberService.getOne(wrapper);
        if (member==null){
            return R.succ().data("flag",0);
        }else{
            return R.succ().data("flag",1);
        }
    }


    @ApiOperation("根据组织活动id显示成员")
    @GetMapping("/getMemberByActivityId")
    public R getMemberByActivityId(@RequestParam String memberActivityId){
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("member_activity_id",memberActivityId);
        List list = memberService.list(wrapper);
        return R.succ().data("memberList",list);
    }
    @ApiOperation("根据用户id显示成员")
    @GetMapping("/getMemberByUserId")
    public R getMemberByUserId(@RequestParam String memberUserId){
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("member_user_id",memberUserId);
        List<Member> list = memberService.list(wrapper);
        List<Activity> activityList = new ArrayList();
        for (Member member:list) {
            Activity activity = activityService.getById(member.getMemberActivityId());
            if (activity!=null){
                activityList.add(activity);
            }
        }
        return R.succ().data("ActivityList",activityList);
    }

    @ApiOperation("管理员查看所有活动成员列表")
    @GetMapping("/getAllMemberAndActivity")
    public R getAllMemberAndActivity(){
        List<Member> list = memberService.list();
        return R.succ().data("memberList",list);
    }

//    @ApiOperation("用户取消报名组织活动,根据id进行删除即可")
//    @PostMapping("/deleteMemberById")
//    public R deleteMemberById(@RequestParam String memberId){
//        Member member = memberService.getById(memberId);
//        String activityId = member.getMemberActivityId();
//        Activity activity = activityService.getById(activityId);
//        activity.setMemberNumber(activity.getMemberNumber()-1);
//        boolean b = memberService.removeById(memberId);
//
//        if (b==true){
//            return R.succ().message("取消报名成功！");
//        }else {
//            return R.fail().message("取消报名失败！");
//        }
//    }



}

